App.controller('stockTransferOutController', ['$rootScope', '$scope', '$state', 'Page','$stateParams', function($rootScope, $scope, $state, Page, $stateParams) {

    $scope.grid = {
        add : function() {
            $scope.type = 1;
            $scope.stockTransferOut = {};
            $scope.modal.setTitle('新增').show();
        },
        update: function(row) {
            $scope.type = 2;
            $scope.stockTransferOut = row;
            $scope.modal.setTitle('编辑').show();
        },
        check: function(row) {
            $scope.type = 3;
            $scope.stockTransferOut = row;
            $scope.modal.setTitle('查看').show();
        },
        save : function() {
            Page.ajaxPost($scope.stockTransferOut.listId ? 'stockTransferOut/update.do' : 'stockTransferOut/add.do', $scope.stockTransferOut, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [{
            target : 14,
            buttons : [
                {
                    name: '查看',
                    class: 'green btm-sm rounded',
                    icon: 'fa-search',
                    event: 'check'
                },
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm rounded',
                    icon: 'fa-edit',
                    event: 'update'
                },
                {
                    name: '删除',
                    class: 'btn-danger rounded',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('stockTransferOut/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

    $scope.gridStock = {
        column_defs: [{
            target : 0,
            buttons : [
                {
                    name: '选择',
                    class: 'btn-warning btm-sm rounded',
                    icon: 'fa-check',
                    event: function(row) {
                        $scope.stockTransferOut.stockId = row.stockId;
                        $scope.stockTransferOut.typeId = row.typeId;
                        $scope.stockTransferOut.typeName = row.typeName;
                        $scope.stockTransferOut.spec = row.spec;
                        $scope.stockTransferOut.brand = row.brand;
                        $scope.stockTransferOut.unit = row.unit;
                        $scope.stockTransferOut.stockName = row.stockName;
                        $scope.modalStock.hide();
                    }
                }
            ]
        }]
    };

    /*计算总价*/
    $scope.$watch('stockTransferOut.num', function(newValue) {
        if( typeof newValue != 'undefined'){
            $scope.stockTransferOut.totalPrice = $scope.stockTransferOut.num * $scope.stockTransferOut.price;
        }
    });

    $scope.$watch('stockTransferOut.price', function(newValue) {
        if( typeof newValue != 'undefined'){
            $scope.stockTransferOut.totalPrice = $scope.stockTransferOut.num * $scope.stockTransferOut.price;
        }
    });

    /*自定义查询*/
    $scope.query = function () {
        $scope.grid.query();
    };

    /*自定义重置*/
    $scope.reset = function () {
        $scope.grid.reset();
    };
}]);